package fxification.java;

import com.sun.javafx.functions.Function0;
import com.sun.javafx.functions.Function1;
import javafx.lang.FX;

/**
 *
 * @author vvivien
 */
public abstract class ATaskRunner implements TaskRunner{
    protected void signalTaskStart(final TaskRunnerContext task) {
        task.setStarted(true);
        task.setStopped(false);
        task.setDone(false);
        task.setSuccessful(false);
        if(task.getStartTaskCallback() != null){
            task.getStartTaskCallback().invoke();
        }
    }

    protected void signalTaskCompletion(final TaskRunnerContext task, final Object result) {
        task.setSuccessful(true);
        task.setDone(true);
        if(task.getTaskSucceededCallback() != null){
            task.getTaskSucceededCallback().invoke(result);
        }
        if(task.getTaskDoneCallback() != null){
            task.getTaskDoneCallback().invoke();
        }
    }

    // enqueue signaling for error
    protected void signalTaskError(final TaskRunnerContext task, final Throwable e) {
        task.setDone(true);
        task.setFailed(true);
        task.setFailureCause(e);
        if(task.getTaskFailedCallback() != null){
            task.getTaskFailedCallback().invoke(e);
        }
        if(task.getTaskDoneCallback() != null){
            task.getTaskDoneCallback().invoke();
        }
    }

    // enqueue signaling for stop
    protected void signalTaskStop(final TaskRunnerContext task) {
        task.setStopped(true);
        task.setDone(true);
        task.setSuccessful(false);
        if(task.getStopTaskCallback() != null){
            task.getStopTaskCallback().invoke();
        }
        if(task.getTaskDoneCallback() != null){
            task.getTaskDoneCallback().invoke();
        }
    }

}
